package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ArticleHomeMapper;
import com.heima.article.service.ArticleService;
import com.heima.article.service.ArticleUploadFreeMarkerHtml;
import com.heima.model.article.dto.ArticleDto;
import com.heima.model.article.pojo.ApArticle;
import com.heima.model.article.pojo.ApArticleConfig;
import com.heima.model.article.pojo.ApArticleContent;
import com.heima.model.dtos.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.net.nntp.Article;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * @author 张一恒
 * @version 1.0
 * @description:
 * @date 2023/1/13 21:29
 */
@Service
@Slf4j
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleHomeMapper articleHomeMapper;

    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;

    @Autowired
    private ApArticleContentMapper apArticleContentMapper;

    @Autowired
    private ArticleUploadFreeMarkerHtml articleUploadFreeMarkerHtml;

    /**
     * @param articleDto
     * @return com.heima.model.dtos.ResponseResult<java.lang.Long>
     * @description 保存文章数据
     * @author 31115
     * @date 2023/1/13 21:31
     */
    @Override
    @Transactional
    public ResponseResult<Long> saveArticle(ArticleDto articleDto) {
        //文章有三张表
        ApArticle article = new ApArticle();
        BeanUtils.copyProperties(articleDto, article);
        ApArticleContent apArticleContent = null;
        //判断文章id是否为空如果为空则是新增 不为空则是修改
        if (StringUtils.isEmpty(articleDto.getId())) {
            //保存ap_article 表
            article.setCreatedTime(new Date());
            articleHomeMapper.insert(article);
            //保存ap_article_config
            ApArticleConfig apArticleConfig = new ApArticleConfig();
            apArticleConfig.setArticleId(article.getId());
            apArticleConfig.setIsComment(true);
            apArticleConfig.setIsForward(true);
            apArticleConfig.setIsDown(false);
            apArticleConfig.setIsDelete(false);
            apArticleConfigMapper.insert(apArticleConfig);
            //保存ap_article_content
            apArticleContent = new ApArticleContent();
            apArticleContent.setArticleId(article.getId());
            apArticleContent.setContent(articleDto.getContent());
            apArticleContentMapper.insert(apArticleContent);
            log.info("保存文章数据保存成功");
        }else {
            //修改文章
            articleHomeMapper.update(article, Wrappers.<ApArticle>lambdaQuery().eq(ApArticle::getId, articleDto.getId()));
            //修改文章内容
            apArticleContent = new ApArticleContent();
            apArticleContent.setContent(articleDto.getContent());
            apArticleContentMapper.update(apArticleContent,
                    Wrappers.<ApArticleContent>lambdaQuery().eq(ApArticleContent::getArticleId, articleDto.getId()));
            log.info("修改文章数据修改成功");
        }

        //生成静态文件上传到分布式文件系统
        articleUploadFreeMarkerHtml.uploadFreeMarkerHtml(article,apArticleContent.getContent());


        return ResponseResult.okResult(article.getId());
    }
}
